package com.app.miwawa.api.esm.entity.customer;

import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.framework.common.api.entity.CommonEntityUUID;
import org.framework.common.api.support.annotation.Chosen;
import org.framework.common.api.support.annotation.ChosenType;
import org.framework.common.api.support.constant.SystemConstants;
import org.framework.common.api.support.model.ProcessModel;
import org.framework.common.ext.util.StringUtil;

import com.app.miwawa.api.system.entity.AppUser;

/**
 * 家长信息表
 * <p>描述: 客户资料</p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年1月23日 下午10:30:25
 * @version 1.0.2016
 */
@Entity @Table(name = "TB_ESM_CUSTOMER_PARENTS")
public class Parents extends CommonEntityUUID {

	/** serialVersionUID(long):. */
	private static final long serialVersionUID = 6311023824090662562L;
	
	// app帐号
	@Column(name = "app", length = 50, nullable = true)
	private String app;
	
	// 姓名
	@Column(name = "name", length = 50, nullable = true)
	private String name;
	
	// 别名
	@Column(name = "alias", length = 50, nullable = true)
	private String alias;
	
	// email
	@Column(name = "email", length = 50, nullable = true)
	private String email;
	
	// 手机
	@Column(name = "mobile", length = 50, nullable = true)
	private String mobile;
	
	// 微信
	@Column(name = "wechat", length = 50, nullable = true)
	private String wechat;
	
	// 关系
	@Column(name = "relation", length = 5, nullable = true)
	private String relation;
	
	// 客户资料ID
	@Column(name = "customer_id", length = 50, nullable = true)
	private String customerId;

	/**
	 * @see org.framework.common.api.entity.CommonEntityUUID#save(org.framework.common.api.service.bo.CommonBoAware, org.springframework.jdbc.core.JdbcTemplate, java.lang.String[])
	 * @author chanlong(陈龙)
	 * @date 2016年8月30日 下午12:55:36
	 */
	@Override
	public Object save(final String... options) throws Exception {
		return save(options[0]);
	}

	/*
	 * 保存家长信息，同时注册环信
	 */
	private Map<String, Object> save(final String dbname) throws Exception{
		// 获取业务操作辅助对象
		ProcessModel process = ProcessModel.factory();
				
		String parentid;
		
		// 新增家长信息
		if(!hasParents(mobile,  id)){
			this.setInvalid(SystemConstants.DICT_DELETE_INVALID);
			parentid = process.bo.update(this).getId();
			
			// 新增家长APP账户并同步环信账号
			if(!hasAppuser(mobile)){
				new AppUser(dbname, email, mobile, alias, relation).save();
			}
		}else{
			return result(RETURN_INVALID, "已存在手机号["+mobile+"]的客户线索。", "");
		}

		return result(RETURN_SUCCESS, "保存家长信息成功！", parentid);
	}
	
	/*
	 * 查找客户信息中的家长信息
	 */
	private boolean hasParents(final String mobile, final String id){
		ProcessModel process = ProcessModel.factory();
		
		StringBuilder sql = new StringBuilder(process.properties.getProperty("customer.parents.has"));
		if(StringUtil.isNotEmpty(id)){
			sql.append(" and id <> ? ");
			return process.jdbc.queryForObject(sql.toString(), Integer.class, mobile, id) > 0;
		}else{
			return process.jdbc.queryForObject(sql.toString(), Integer.class, mobile) > 0;
		}
	}
	
	/*
	 * 查找家长APP账户
	 */
	private boolean hasAppuser(final String mobile){
		ProcessModel process = ProcessModel.factory();
		return process.jdbc.queryForObject(process.properties.getProperty("customer.appuser.has"), Integer.class, mobile, mobile) > 0;
	}
	
	public Parents() {

	}
	
	@Override
	@Chosen(type=ChosenType.VALUE)
	public String getId() {
		// TODO Auto-generated method stub
		return super.getId();
	}

	public String getApp() {
		return app;
	}

	public void setApp(String app) {
		this.app = app;
	}
	
	@Chosen(type=ChosenType.ATTRIBUTE)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Chosen(type=ChosenType.ATTRIBUTE)
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getWechat() {
		return wechat;
	}

	public void setWechat(String wechat) {
		this.wechat = wechat;
	}

	@Chosen(type=ChosenType.TEXT)
	public String getRelation() {
		return relation;
	}

	public void setRelation(String relation) {
		this.relation = relation;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	
}
